package com.zhenyi.xhc.core;

import java.io.File;
import java.util.Date;
import java.util.concurrent.ExecutorService;

import org.joda.time.DateTime;

import com.jfinal.kit.StrKit;
import com.utils.DateUtils;
import com.utils.PathUtils;
import com.xhc.common.model.XhcTicket;
import com.zhenyi.api.file.QNUtils;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpUtil;

public class TicketUtils {
	private void genDB(DateTime dateTime, int time) {
		pool.execute(new Runnable() {

			@Override
			public void run() {
				String str = DateUtils.dateFormat(dateTime.toDate(), DateUtils.YYYYMMDD);
				str = str + " {}:00";
				str = StrFormatter.format(str, time);
				Date ticketTime = DateUtils.dateFromString(str, DateUtils.YYYYMMDD_HHMM);
				XhcTicket ticket = new XhcTicket();
				ticket.setTime(ticketTime);
				ticket.save();
			}
		});
	}

	public static String getQRCode(String code) {
		try {
			String qrcodeImageUrl = "http://release.zhen-yee.com/api/qrcode?lt=1&size=100&text=" + code;
			String path = PathUtils.getFile("data", "xhc", "qrcodeImage" + code + ".png");
			File file = new File(path);
			if (!file.exists()) {
				HttpUtil.downloadFile(qrcodeImageUrl, path);
				// return "http://lets.zhen-yee.com/tkQrcode/" + code + ".png";
			}
			QNUtils qnUtils = new QNUtils("lets", "z2AFlN5J711q7CcKbabHakkrsj6IyBsBSqpSw_Kg",
					"QsjUX9QDnUrGxpgBWmnY8T6-ciGCdhhYIXJaauT0");
			String qnurl = qnUtils.upload(file, "tkQrcode/" + code + ".png");
			return "http://lets.zhen-yee.com/" + qnurl;
		} catch (Exception e) {
			return "";
		}
	}

	public static String qrcodeImage(String code, String no, String ticketTitle, String time) {
		String url = "https://release.zhen-yee.com/xhc/ticket/bg.png?watermark/3/image/{}/"
				+ "dissolve/100/gravity/NorthEast/dx/75/dy/150/ws/0.3/wst/0"
				+ "/text/{}/font/6buR5L2T/fontsize/1200/fill/d2hpdGU=/dissolve/100/gravity/"
				+ "NorthWest/dx/100/dy/180/text/{}/font/6buR5L2T/fontsize/1200/fill/d2hpdGU=/dissolve/100/gravity/NorthWest/dx/220/dy/250/"
				+ "text/{}/font/6buR5L2T/fontsize/550/fill/d2hpdGU=/dissolve/100/gravity/SouthEast/dx/50/dy/170"
				+ "/text/{}/font/6buR5L2T/fontsize/1150/fill/d2hpdGU=/dissolve/100/gravity/South/dx/0/dy/60"
				+ "|imageView2/0/w/400/h/400/q/75" + "|imageMogr2/rotate/90";
		// String qrcodeImageUrl = "http://www.liantu.com/api.php?w=100&text=" +
		// code;
		String qrcodeImageUrl = getQRCode(code);
		if (StrKit.isBlank(qrcodeImageUrl)) {
			return "";
		}
		qrcodeImageUrl = Base64.encode(qrcodeImageUrl);
		String title = "出出帮火车奇妙世界";
		title = Base64.encode(title);
		title = title.replace("+", "-");
		title = title.replace("/", "_");
		// String ticketTitle = "抢先体验票";
		ticketTitle = Base64.encode(ticketTitle);
		ticketTitle = ticketTitle.replace("+", "-");
		ticketTitle = ticketTitle.replace("/", "_");

		String ticketNo = "票号码:" + no;
		ticketNo = Base64.encode(ticketNo);
		ticketNo = ticketNo.replace("+", "-");
		ticketNo = ticketNo.replace("/", "_");

		String ticketTime = time;
		ticketTime = Base64.encode(ticketTime);
		ticketTime = ticketTime.replace("+", "-");
		ticketTime = ticketTime.replace("/", "_");

		url = StrFormatter.format(url, qrcodeImageUrl, title, ticketTitle, ticketNo, ticketTime);
		return url;
	}

	public static void main(String[] args) {
		System.out.println(qrcodeImage("7XyQK8wD", "2018122709430259307a", "抢先体验票", "时间: 2018-01-16 09:00-10:00"));
	}

	ExecutorService pool = ThreadUtil.newExecutor(20);

	public void genTicket() {
		Date dt = DateUtils.dateFromString("2019-03-15 00:00:00", DateUtils.YYYYMMDD_HHMMSS);
		DateTime dateTime = new DateTime(dt);
		boolean isEnd = false;
		DateTime endDate = dateTime.plusYears(10);
		isEnd = endDate.isBefore(dateTime);
		System.out.println(dateTime.toDate());
		System.out.println(endDate.toDate());
		System.out.println(new Date());
		while (!isEnd) {
			for (int time = 0; time < 24; time++) {
				genDB(dateTime, time);
				// String str = DateUtils.dateFormat(dateTime.toDate(),
				// DateUtils.YYYYMMDD);
				// str = str + " {}:00";
				// str = StrFormatter.format(str, time);
				// Date ticketTime = DateUtils.dateFromString(str,
				// DateUtils.YYYYMMDD_HHMM);
				// XhcTicket ticket = new XhcTicket();
				// ticket.setTime(ticketTime);
				// ticket.save();
			}
			// System.out.println(dateTime.toDate());
			dateTime = dateTime.plusDays(1);
			isEnd = endDate.isBefore(dateTime);
		}
		System.out.println(new Date());
	}
}
